package cn.iocoder.yudao.module.signature.controller.app.developeraccount;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.signature.controller.admin.developeraccount.vo.DeveloperAccountPageReqVO;
import cn.iocoder.yudao.module.signature.controller.admin.developeraccount.vo.DeveloperAccountRespVO;
import cn.iocoder.yudao.module.signature.controller.admin.developeraccount.vo.DeveloperAccountSaveReqVO;
import cn.iocoder.yudao.module.signature.controller.app.developeraccount.vo.SyncDeviceReqVO;
import cn.iocoder.yudao.module.signature.enums.AppleAccountType;
import cn.iocoder.yudao.module.signature.service.developeraccount.DeveloperAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "app - 苹果开发者账号")
@RestController
@RequestMapping("/signature/developer-account")
@Validated
public class AppDeveloperAccountController {

    @Resource
    private DeveloperAccountService developerAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建苹果开发者账号")
    @PreAuthenticated
    public CommonResult<Long> createDeveloperAccount(@Valid @RequestBody DeveloperAccountSaveReqVO createReqVO) throws Exception {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        createReqVO.setUserId(loginUser.getId());
        createReqVO.setAccountBelongType(String.valueOf(AppleAccountType.INDEPENDENT_POOL.getType()));

        return success(developerAccountService.createDeveloperAccount(createReqVO));
    }

    @PostMapping("/refreshRemaining")
    @Operation(summary = "刷新余量")
    @PreAuthenticated
    public CommonResult<Boolean> createDeveloperAccount(@RequestParam("id") Long id) throws Exception {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        developerAccountService.refreshRemaining(id, loginUser.getId());
        return success(true);
    }

    @PostMapping("/downloadMobileProvision")
    @Operation(summary = "下载描述文件")
    @PreAuthenticated
    public CommonResult<Boolean> downloadMobileProvision(@RequestParam("id") Long id) throws Exception {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        developerAccountService.downloadMobileProvision(id, loginUser.getId());
        return success(true);
    }


    @PutMapping("/update")
    @Operation(summary = "更新苹果开发者账号")
    @PreAuthenticated
    public CommonResult<Boolean> updateDeveloperAccount(@Valid @RequestBody DeveloperAccountSaveReqVO updateReqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        updateReqVO.setUserId(loginUser.getId());
        developerAccountService.updateDeveloperAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除苹果开发者账号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthenticated
    public CommonResult<Boolean> deleteDeveloperAccount(@RequestBody List<Long> ids) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        developerAccountService.deleteDeveloperAccount(ids,loginUser.getId());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得苹果开发者账号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<DeveloperAccountRespVO> getDeveloperAccount(@RequestParam("id") Long id) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        return success(developerAccountService.getDeveloperAccount(id,loginUser.getId()));
    }

    @GetMapping("/page")
    @Operation(summary = "获得苹果开发者账号分页")
    @PreAuthenticated
    public CommonResult<PageResult<DeveloperAccountRespVO>> getDeveloperAccountPage(@Valid DeveloperAccountPageReqVO pageReqVO) {
             LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        pageReqVO.setUserId(loginUser.getId());
        return success(developerAccountService.getDeveloperAccountPage(pageReqVO));
    }


    @PostMapping("/syncDevice")
    @Operation(summary = "同步设备")
    @PreAuthenticated
    public CommonResult<Boolean> syncDevice(@RequestBody SyncDeviceReqVO reqVO) throws Exception {
        developerAccountService.syncDevice(reqVO);
        return success(true);
    }

}